﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IosDatamodel;
using System.Web.Mvc;
using System.Data.Entity;
using System.Data;
using IOSpro.Lib;
using IOSpro.Models;
namespace IOSpro.Lib
{

    public class UsersBL : IUsersBL
    {
        private IosDBEntities db;
        private ModelStateDictionary _errorLog;


        public UsersBL(ModelStateDictionary modelState)
        {
            _errorLog = modelState;
            db = new IosDBEntities();
        }
        public int GetAllUsersCount()
        {

            return 3;

        }
        public bool GetUsersVerificationCount(string verification)
        {
            if (db.Users.Where(b => b.Verification == verification).Count() >= 3)
            {
              //  _errorLog.AddModelError("", "激活数量已达上限");
                return false;
            }
            else
            {
               
                return true;
            }
            
        }
    public    bool ValidateYesorNot(string email) 
        {
            if (db.Users.Where(b => b.Email == email).FirstOrDefault().Verification ==null) return false;
            else return true;
        
        
        
        }
        public bool SaveUsers(string email ,string password)
        {
            try
            {
                Users users = new Users
                {
                    Email = email,
                    PassWord = password

                };
                db.Users.Add(users);
                db.SaveChanges();
                return true;
            }
            catch { return false; }
        }

       public  bool RepeatUser(string email) 
        {
            if (db.Users.Where(a => a.Email == email).Count() > 0) return false;
            return true;    
        }
    public bool LogOnUser( string email,string password)
       {
           if (db.Users.Where(a => a.Email == email && a.PassWord == password).Count() > 0) return true;
           return false;
              }
    public bool ValidateUser(string email, string validatestring) {


        Users user = db.Users.Where(a => a.Email == email).FirstOrDefault();

        user.Verification = validatestring;
         db.Users.Attach(user);
         db.Entry(user).State = EntityState.Modified;
        db.SaveChanges();
        return true;
    }
        //public BulletinBL(ModelStateDictionary modelState)
        //{
        //    _errorLog = modelState;
        //    db = new IosDBEntities();
        //}
        //public IQueryable<Users> GetBulletinList()
        //{

        //    return db.Bulletins.OrderByDescending(a => a.ID);
        //}
        //public IQueryable<Bulletin> GetAnonymousBulletinList()
        //{

        //    return db.Bulletins.Where(b=>b.Visible==true&&b.StartDate<DateTime.Now&&b.DueDate>DateTime.Now).OrderByDescending(a => a.ID);      
        //}


        //public bool DeleteBulletin(int id)
        //{
        //    if (db.BulletinItems.Where(b => b.BulletinID == id).Count() > 0)
        //    {
        //        _errorLog.AddModelError("", "该项目已经有子项目!请先删除子项目!");
        //        return false;
        //    }
        //    else
        //    {
        //        Bulletin bulletin = db.Bulletins.Where(a => a.ID == id).Single();
        //        db.Bulletins.Remove(bulletin);
        //        db.SaveChanges();
        //        return true;
        //    }



        //}

        //public void saveBulletin(BulletinCreateModel savebulletin)
        //{
        //    Bulletin bulletin = new Bulletin
        //    {
        //        Name = savebulletin.name,
        //        StartDate = savebulletin.startdatetime,
        //        DueDate = savebulletin.Duedatetime,
        //        StarFeedbackEnabled = savebulletin.StarFeedbackEnabled,
        //        TextFeedbackEnabled = savebulletin.TextFeedbackEnabled,
        //        Visible = savebulletin.visible,
        //        Description = savebulletin.Description

        //    };
        //        db.Bulletins.Add(bulletin);
        //        db.SaveChanges();
        //}
        //public void comparedatetime( DateTime startime ,DateTime duetime)
        //{

        //    if (startime <= duetime) { 


        //    }


        //}

        //    public void SaveEditBulletin(int BulletinID,  BulletinCreateModel savebulletin)
        //    {
        //        Bulletin bulletin = new Bulletin
        //        {
        //             ID=BulletinID,
        //              Name=savebulletin.name,
        //               StartDate=savebulletin.startdatetime,
        //                DueDate=savebulletin.Duedatetime,
        //                 StarFeedbackEnabled=savebulletin.StarFeedbackEnabled,
        //                  TextFeedbackEnabled=savebulletin.TextFeedbackEnabled,
        //                   Visible=savebulletin.visible,
        //                    Description=savebulletin.Description


        //        };
        //        db.Bulletins.Attach(bulletin);
        //        db.Entry(bulletin).State = EntityState.Modified;
        //        db.SaveChanges();

        //    }
        //    public int GetBulletinCount() 
        //    {
        //        return db.Bulletins.Count();


        //    }
        //  public  BulletinCreateModel DeleteDetail(int id) 
        //    {
        //        var bulletin = from a in db.Bulletins
        //                       where a.ID == id
        //                       select new BulletinCreateModel
        //                       {
        //                           name = a.Name,
        //                           startdatetime = a.StartDate,
        //                           Duedatetime = a.DueDate,
        //                           StarFeedbackEnabled = a.StarFeedbackEnabled,
        //                           TextFeedbackEnabled = a.TextFeedbackEnabled,
        //                           Description = a.Description,
        //                           visible = a.Visible
        //                       };
        //        return bulletin.SingleOrDefault();

        //    }
        // public    String GetBulletinName(int BulletinId) 
        //  {
        //      return db.Bulletins.Where(a => a.ID == BulletinId).SingleOrDefault().Name.ToString();
        //  }
        //  public   String GetBulletinDescription(int BulletinId) 
        //  {
        //     // return db.Bulletins.Where(a => a.ID == BulletinId).SingleOrDefault().Description.ToString();
        //      var description = (from a in db.Bulletins where a.ID == BulletinId select a.Description).SingleOrDefault();
        //      if (description == null)
        //          return "";
        //      else return description.ToString();

        //  }



        //}
    }
}